﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;
using System.Text.RegularExpressions;
using Microsoft.Reporting.WinForms;

namespace QLTB_CTK34
{
    public partial class FrmThongKe : Form
    {
        public FrmThongKe()
        {
            InitializeComponent();
        }

        private void FrmThongKe_Load(object sender, EventArgs e)
        {

            this.RpvThongKe.RefreshReport();
        }
        private enum DKThongKe
        {
            ThietBi =0,
            ThongBaoLoi,
            KetQuaBT
        };
        /// <summary>
        /// Khi click vào button Thống Kê Sẽ Hiện Thông Tin THống Kê Theo Điều Kiện.
        /// </summary>
        private void btnThongKe_Click(object sender, EventArgs e)
        {
            ThongKe((DKThongKe)cbbFrom.SelectedIndex,ds);
            this.RpvThongKe.RefreshReport();
        }

        /// <summary>
        /// Hàm load dữ liệu theo dk truyền vô.
        /// </summary>
        private void ThongKe(DKThongKe dk,IEnumerable<object> ds)
        {
            DataTable tb= new DataTable();
            RpvThongKe.LocalReport.DataSources.Clear();
            ThuVien.TRY_CATCH(() =>
                {
                    switch (dk)
                    {
                        case DKThongKe.ThietBi:
                            tb = new Reporting.ThietBiDataTable();
                            ds.ToList().ForEach(x1 =>
                                {
                                    var x = (ThietBi)x1;
                                    tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang);
                                });
                            RpvThongKe.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.ThietBi.rdlc";
                            this.RpvThongKe.LocalReport.DataSources.Add(new ReportDataSource("ThietBi", (DataTable)tb));
                            RpvThongKe.LocalReport.Refresh();
                            break;
                        case DKThongKe.ThongBaoLoi:
                            tb = new Reporting.TBLoiTBDataTable();
                            ds.ToList().ForEach(x1 =>
                                {
                                    var x = (ThongBaoLoiThietBi)x1;
                                    tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu);
                                });
                            RpvThongKe.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.TBLoiTB.rdlc";
                            this.RpvThongKe.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", (DataTable)tb));
                            RpvThongKe.LocalReport.Refresh();
                            break;
                        case DKThongKe.KetQuaBT:
                                     tb = new Reporting.KetQuaBaoTriDataTable();
                            ds.ToList().ForEach(x1 =>
                                {
                                    var x = (KQBaoTriTB)x1;
                                    tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua);
                                });
                            RpvThongKe.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.KQBaoTri.rdlc";
                            this.RpvThongKe.LocalReport.DataSources.Add(new ReportDataSource("KetQuaBT", (DataTable)tb));
                            RpvThongKe.LocalReport.Refresh();
                            break;
                        default:
                            break;
                    }
                });
            
        }
        public Type type;
        public IEnumerable<object> ds;
        /// <summary>
        /// Hàm Load Dk Cho cbbDKLoc
        /// </summary>
        private void cbbFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable tb;
            switch ((DKThongKe)cbbFrom.SelectedIndex)
            {
                case DKThongKe.ThietBi:
                    type = typeof(ThietBi);
                    ds = QLThietBi.GetAll();
                     tb = new Reporting.ThietBiDataTable();
                    break;
                case DKThongKe.ThongBaoLoi:
                    type = typeof(ThongBaoLoiThietBi);
                    ds = QLTBLoiThietBi.GetAll();
                     tb = new Reporting.TBLoiTBDataTable();
                    break;
                case DKThongKe.KetQuaBT:
                    type = typeof(KQBaoTriTB);
                    ds = QLKQBaoTriTB.GetAll();
                    tb = new Reporting.KetQuaBaoTriDataTable();
                    break;
                default:
                    break;
            }
        }
        public void XuLiDuLieu(List<object> ds)
        {
            frmdsdk.ds = ds;
            ThongKe((DKThongKe)cbbFrom.SelectedIndex, ds);
            this.RpvThongKe.RefreshReport();
        }
        FrmDanhSachDK frmdsdk;
        private void btndsdk_Click(object sender, EventArgs e)
        {
            frmdsdk = new FrmDanhSachDK();
            frmdsdk.type = type;
            frmdsdk.ds = ds;
            frmdsdk.ucCompare1.InitColumnName(type, null, ds);
            frmdsdk.ucCompare1.xldt = XuLiDuLieu;
            frmdsdk.Show();
        }
    }
}
